package com.jcxy.service.impl;

import com.jcxy.VO.AttendanceVo;
import com.jcxy.VO.UserVo;
import com.jcxy.mapper.AttendanceMapper;
import com.jcxy.pojo.Attendance;
import com.jcxy.service.AttendanceService;
import com.jcxy.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class AttendanceServiceImpl implements AttendanceService {

    @Autowired
    private AttendanceMapper attendanceMapper;

    @Override
    @Transactional
    public void insertAttendance(Attendance attendance) throws Exception {

        //通过考勤日期和类型，判断是否存在当天的考勤
        Attendance a = attendanceMapper.queryTodayAttendanceByType(attendance);
        if (a != null){
            //存在当天该类型考勤，更新该考勤时间和状态
            int count = attendanceMapper.updateAttendance(attendance);
            if (count > 1){
                throw new Exception("更新考勤时间和状态失败");
            }
        }else {
            //不存在，则插入考勤信息
            attendanceMapper.insetAttendance(attendance);
        }

    }

    @Override
    public PageBean queryListByPage(Map dto, Integer page, Integer limit) {

        Integer count = attendanceMapper.queryAttendanceCount(dto);
        PageBean pageBean = new PageBean(page, limit);
        dto.put("pageIndex",pageBean.getPageIndex());
        dto.put("limit",pageBean.getLimit());
        List<AttendanceVo> attendanceList = attendanceMapper.queryAttendance(dto);

        pageBean.setResponseData(count,attendanceList);
        return pageBean;
    }

    @Override
    @Transactional
    public int updateAttendance(Map dto) throws Exception {

        int count = attendanceMapper.updateAttendanceById(dto);
        if(count > 1){
            throw new Exception("更新考勤信息异常！");
        }
        return count;
    }

    @Override
    @Transactional
    public int deleteAttendance(Integer attendance_id) throws Exception {

        int count = attendanceMapper.deleteAttendance(attendance_id);
        if(count > 1){
            throw new Exception("删除考勤信息异常！");
        }
        return count;
    }
}
